/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.me.mobisegua;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import java.util.List;

/**
 *
 * @author Zé Carlos
 */
public class MapViewActivity extends MapActivity implements LocationListener {

    LinearLayout linearLayout;
    MapView mapView;
    List<Overlay> mapOverlays;
    Drawable drawable;
    MapsItemizedOverlay itemizedOverlay;
    MapController mc;
    LocationManager lm;
    GeoPoint point;

    //String lat;
    //String lon;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.map);

        //Toast.makeText(MapViewActivity.this, "Toast!", Toast.LENGTH_LONG).show();

        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

        linearLayout = (LinearLayout) findViewById(R.id.zoomview);
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);

        mapView.setEnabled(true);
        //mapView.setSatellite(true);

        mc = this.mapView.getController();
        point = new GeoPoint((int) (40.646420 * 1E6), (int) (-8.589200 * 1E6));

        /*MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
        myLocationOverlay.enableCompass();
        myLocationOverlay.enableMyLocation();
        mapView.getOverlays().add(myLocationOverlay);*/
        mc.setCenter(point);
        mc.setZoom(18);

        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);

        mapView.invalidate();













        mapView.setOnLongClickListener(new OnLongClickListener() {

            public boolean onLongClick(View arg0) {
                throw new UnsupportedOperationException("Not supported yet.");

                // Lanças uma actividade "AddSituation"
            }
        });





        /*mapOverlays = mapView.getOverlays();
        MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
        myLocationOverlay.enableMyLocation();
        mapOverlays.add(myLocationOverlay);*/



        //drawable = this.getResources().getDrawable(R.drawable.premium_star);
        //itemizedOverlay = new MapsItemizedOverlay(drawable);


        //int lat_i = Integer.valueOf(this.lat);
        //int lon_i = Integer.valueOf(this.lon);

        /** Generate a new GeoPoint based on the received coordinates. */
        //GeoPoint point = new GeoPoint(lat_i,lon_i);
        //OverlayItem overlayitem = new OverlayItem(point, "", "");
        //itemizedOverlay.addOverlay(overlayitem);
        //mapOverlays.add(itemizedOverlay);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }


    /* This method is called when use position will get changed */
    public void onLocationChanged(Location location) {
        if (location != null) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
            mc.animateTo(point);
            //mc.setCenter(p);
            mc.setZoom(18);

            MapOverlay mapOverlay = new MapOverlay();
            List<Overlay> listOfOverlays = mapView.getOverlays();
            listOfOverlays.clear();
            listOfOverlays.add(mapOverlay);

            mapView.invalidate();
        }
        Toast.makeText(MapViewActivity.this, "Location has changed!", Toast.LENGTH_SHORT).show();
    }

    public void onProviderDisabled(String provider) {
        Toast.makeText(MapViewActivity.this, "onProviderDisabled", Toast.LENGTH_SHORT).show();
    }

    public void onProviderEnabled(String provider) {
        Toast.makeText(MapViewActivity.this, "onProviderEnabled", Toast.LENGTH_SHORT).show();
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        Toast.makeText(MapViewActivity.this, "onStatusChanged", Toast.LENGTH_SHORT).show();
    }

    class MapOverlay extends com.google.android.maps.Overlay {

        @Override
        public boolean draw(Canvas canvas, MapView mapView,
                boolean shadow, long when) {
            super.draw(canvas, mapView, shadow);

            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(point, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pinpoint);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
            return true;
        }
    }
}
